package primetest.impl.service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.inject.Inject;

import primetest.dto.MedicineDto;
import primetest.impl.dao.MedicineDao;
import primetest.impl.dao.Transaction;
import primetest.impl.service.MedicineService;

public class MedicineService implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -2425578018416960291L;
	/**
	 *
	 */

	private Transaction transaction;
	private MedicineDao medicineDao;

	@Inject
	public MedicineService(Transaction transaction,MedicineDao medicineDao){
		this.transaction = transaction;
		this.medicineDao = medicineDao;
	}

	public List<MedicineDto> getMedicineList() throws IOException {
		try{
			transaction.begin();
			List<MedicineDto> medicineList = medicineDao.getAllList(transaction);
			//transaction.commit();
			return medicineList;
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}



	public void addMedicine(MedicineDto medicineDto) throws IOException {
		try{
			transaction.begin();
			medicineDao.addMedicine(transaction, medicineDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void updateMedicine(MedicineDto medicineDto) throws IOException {
		try{
			transaction.begin();
			medicineDao.updateMedicine(transaction, medicineDto);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}

	public void modifyQuantity(MedicineDto key, int q) throws IOException {
		try{
			transaction.begin();
			medicineDao.modifyQuantity(transaction, key, q);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}


	public void deleteMedicine(String medicineSeq) throws IOException {
		try{
			transaction.begin();
			medicineDao.deleteMedicine(transaction, medicineSeq);
			transaction.commit();
		}catch(IOException e){
			transaction.rollback();
			throw e;
		}
	}
}
